package com.atguigu.guli.service.video.controller.admin;

import com.atguigu.commonutils.ExceptionUtils;
import com.atguigu.commonutils.ResultCodeEnum;
import com.atguigu.commonutils.result.R;
import com.atguigu.guli.service.base.Exception.GuliException;
import com.atguigu.guli.service.video.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bytecode.Throw;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author: Mr.Wang
 * @date: 2021-06-02 21:53
 * @description:
 */
@Api(tags = "视频上传")
@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/admin/video/media")
public class VideoController {
    @Autowired
    private VideoService videoService;

    @PostMapping("/upload")
    public R uploadVideo(
            @ApiParam(value = "视频文件",required = true)
            @RequestParam MultipartFile file
            ) {
        try {
            String originalFilename = file.getOriginalFilename();
            InputStream inputStream = file.getInputStream();
            String video_source_Id = videoService.uploadVideo(inputStream, originalFilename);
            return R.ok().message("视频上传成功").data("video_source_Id",video_source_Id);
        } catch (IOException e) {
           log.error(ExceptionUtils.getMessage(e));
           throw new GuliException(ResultCodeEnum.VIDEO_UPLOAD_ALIYUN_ERROR);
        }
    }

    @DeleteMapping("/remove/{videoId}")
    public R removeVideo(
            @ApiParam(value = "视频Id",required = true)
            @RequestParam String videoId
    ) {
        try {

            videoService.removeVideo(videoId);
            return R.ok().message("删除成功");
        } catch (Exception e) {
           log.error(ExceptionUtils.getMessage(e));
           throw new GuliException(ResultCodeEnum.VIDEO_DELETE_ALIYUN_ERROR);
        }
    }
}
